﻿using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace Borg
{
    public class OrderedDictionary<TKey, TValue> : IDictionary<TKey, TValue>, IList<KeyValuePair<TKey, TValue>>
    {
        private readonly Dictionary<TKey, TValue> _itemsDictionary;

        private readonly List<TKey> _keyList;
        private readonly List<TValue> _valueList;

        private readonly ICollection<TKey> _keyReadOnlyCollection;
        private readonly ICollection<TValue> _valueReadOnlyCollection;

        public int Count
        {
            get { return _keyList.Count; }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public TValue this[TKey key]
        {
            get { return _itemsDictionary[key]; }
            set
            {
                if (_itemsDictionary.ContainsKey(key))
                {
                    var index = _keyList.IndexOf(key);
                    _keyList[index] = key;
                    _valueList[index] = value;
                }
                else
                {
                    _keyList.Add(key);
                    _valueList.Add(value);
                }
                
                _itemsDictionary[key] = value;
            }
        }

        public ICollection<TKey> Keys
        {
            get { return _keyReadOnlyCollection; }
        }

        public ICollection<TValue> Values
        {
            get { return _valueReadOnlyCollection; }
        }

        public KeyValuePair<TKey, TValue> this[int index]
        {
            get { return new KeyValuePair<TKey, TValue>(_keyList[index], _valueList[index]); }
            set
            {
                _itemsDictionary.Remove(_keyList[index]);
                _itemsDictionary[value.Key] = value.Value;
                _keyList[index] = value.Key;
                _valueList[index] = value.Value;
            }
        }

        public OrderedDictionary()
        {
            _itemsDictionary = new Dictionary<TKey, TValue>();
            
            _keyList = new List<TKey>();
            _valueList = new List<TValue>();
            
            _keyReadOnlyCollection = new ReadOnlyCollection<TKey>(_keyList);
            _valueReadOnlyCollection = new ReadOnlyCollection<TValue>(_valueList);
        }

        public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
        {
            IEnumerator<TKey> key = _keyList.GetEnumerator();
            IEnumerator<TValue> value = _valueList.GetEnumerator();
            
            while (key.MoveNext() && value.MoveNext())
                yield return new KeyValuePair<TKey, TValue>(key.Current, value.Current);
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        public void Add(KeyValuePair<TKey, TValue> item)
        {
            Add(item.Key, item.Value);
        }

        public void Clear()
        {
            _itemsDictionary.Clear();
            _keyList.Clear();
            _valueList.Clear();
        }

        public bool Contains(KeyValuePair<TKey, TValue> item)
        {
            return _itemsDictionary.ContainsKey(item.Key);
        }

        public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
        {
            foreach (var pair in this)
            {
                array[arrayIndex] = pair;
                ++arrayIndex;
            }
        }

        public bool Remove(KeyValuePair<TKey, TValue> item)
        {
            return
                _itemsDictionary.Remove(item.Key) &&
                _keyList.Remove(item.Key) &&
                _valueList.Remove(item.Value);
        }

        public bool ContainsKey(TKey key)
        {
            return _itemsDictionary.ContainsKey(key);
        }

        public void Add(TKey key, TValue value)
        {
            _itemsDictionary.Add(key, value);
            _keyList.Add(key);
            _valueList.Add(value);
        }

        public bool Remove(TKey key)
        {
            return
                _itemsDictionary.ContainsKey(key) &&
                Remove(new KeyValuePair<TKey, TValue>(key, _itemsDictionary[key]));
        }

        public bool TryGetValue(TKey key, out TValue value)
        {
            if (_itemsDictionary.ContainsKey(key))
            {
                value = _itemsDictionary[key];
                return true;
            }
            
            value = default(TValue);
            
            return false;
        }

        public int IndexOf(KeyValuePair<TKey, TValue> item)
        {
            return _keyList.IndexOf(item.Key);
        }

        public void Insert(int index, KeyValuePair<TKey, TValue> item)
        {
            _itemsDictionary.Add(item.Key, item.Value);
            _keyList.Insert(index, item.Key);
        }

        public void RemoveAt(int index)
        {
            _itemsDictionary.Remove(_keyList[index]);
            _keyList.RemoveAt(index);
            _valueList.RemoveAt(index);
        }
    }
}
